package kcms.common.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import kcms.common.dao.CommonDao;
import kcms.common.service.CommonService;
import kcms.db.Token;
import kcms.db.ViewAssignScore;
import kcms.db.ViewMessage;
import kcms.user.dao.UserDAO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class CommonServiceImpl implements CommonService{
	@Resource
	private CommonDao commondao;
	@Resource
	private Token token;
	@Resource
	private UserDAO userdao;
	
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)

	public CommonDao getCommondao() {
		return commondao;
	}

	public void setCommondao(CommonDao commondao) {
		this.commondao = commondao;
	}
	
	public Token getToken() {
		return token;
	}

	public void setToken(Token token) {
		this.token = token;
	}

	public UserDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(UserDAO userdao) {
		this.userdao = userdao;
	}

	@Override
	public List<ViewMessage> getMessagesSize(String sid) {
		// TODO Auto-generated method stub
		return this.commondao.getMessagesSize(sid);
	}

	@Override
	public List<ViewAssignScore> getAssignMessagesSize(String sid) {
		// TODO Auto-generated method stub
		return this.commondao.getAssignMessagesSize(sid );
	}
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void saveToken(String token,String uid){
		this.token.setUid(uid);
		this.token.setToken(token);
		this.token.setExceeddate(new Date());
		this.commondao.saveToken(this.token);
	}
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public boolean verify(String token){
		
		Token temptoken;
		if((temptoken=this.commondao.getTokenObj(token))!=null){
			userdao.setValid(temptoken.getUid());
			this.commondao.delTokenObj(temptoken);
			return true;
		}
		else {
			return false;
		}
	}
}
